package com.bum.api.processor.dev.json.bo;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;

import com.bum.api.core.exp.om.Metas;
import com.bum.api.core.exp.om.Params;
import com.google.gson.Gson;

public class DevJsonBO {
	Gson	gson;

	public String base64Decode(String path, Params params, Metas metas) {
		String source = params.getValue("source");
		String charset = params.getValue("charset");

		String result = null;
		try {
			result = new String(Base64.decodeBase64(source.getBytes()), charset);
		} catch (UnsupportedEncodingException e) {
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "decode");
		map.put("result", result);
		return gson.toJson(map);
	}

	public String base64Encode(String path, Params params, Metas metas) {
		String source = params.getValue("source");
		String charset = params.getValue("charset");

		String result = null;
		try {
			result = Base64.encodeBase64URLSafeString(source.getBytes(charset));
		} catch (UnsupportedEncodingException e) {
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "encode");
		map.put("result", result);
		return gson.toJson(map);
	}

	public String urlDecode(String path, Params params, Metas metas) {
		String source = params.getValue("source");
		String charset = params.getValue("charset");

		String result = null;
		try {
			result = URLDecoder.decode(source, charset);
		} catch (UnsupportedEncodingException e) {
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "decode");
		map.put("result", result);
		return gson.toJson(map);
	}

	public String urlEncode(String path, Params params, Metas metas) {
		String source = params.getValue("source");
		String charset = params.getValue("charset");

		String result = null;
		try {
			result = URLEncoder.encode(source, charset);
		} catch (UnsupportedEncodingException e) {
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "encode");
		map.put("result", result);
		return gson.toJson(map);
	}

	public void setGson(Gson gson) {
		this.gson = gson;
	}
}
